#ifndef DBTOOL_H
#define DBTOOL_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QPixmap>
#include <QStringList>

#include "publictool.h"
#include "publicdataclass.h"

class TableInfo{
public:
    int cid;
    QString name;
    QString type;

    TableInfo(QSqlQuery& query){fromQuery(query);}
    void fromQuery(QSqlQuery& query){
        cid = query.value("cid").toString().toInt();
        name = query.value("name").toString();
        type = query.value("type").toString();
    }
};


class DBTool : public QObject
{
    Q_OBJECT
public:
    explicit DBTool(QObject *parent = nullptr);
    ~DBTool();

    bool exec(QSqlQuery &query);

    void test();

    QString errString;

    // 登录
    bool log(DB_userInfo info);

    //查询所有学生信息
    QList<DB_View_StudentInfo> getAllStudentInfo(const QString where = "");

    // 查询所有的学生成绩
    QList<DB_View_StudentGrades> getAllStudentGrades(const QString where = "");

    // 查询所有的班级信息
    QList<DB_View_ClassInfo> getAllClassInfo(const QString where = "");

    // 查询所有的用户信息
    QList<DB_userInfo> getAllUserInfo(const QString where = "");

    // 查询创建班级需要的必要信息
    DB_CreateClassInfo getCreateClassInfo();

    // 查询信息
    QList<QVariantList> getTableInfo(const QString &table,const QString where = "");

    // 插入信息
    bool insertTableInfo(const QString &table,
                         const QList<QVariantList>& data,
                         QSqlQuery* query = nullptr);

    // 删除信息
    bool deleteTableInfo(const QString &table,
                         const QString where = "",
                         QSqlQuery* query = nullptr);

    bool updateTableInfo(const QString &table,
                         const QString &set,
                         const QString where = "",
                         QSqlQuery* query = nullptr);

private:

    void open();

    int getTableColCount(QString tableName);

    QList<TableInfo> getTableColInfo(QString tableName);




private:
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

};

#endif // DBTOOL_H
